package com.qsw.httpnetlib.test;

import android.app.Application;
import android.util.Log;

import com.qsw.httpnetlib.net.core.NetModel;
import com.qsw.httpnetlib.net.core.callback.OnNetCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该APP内的网络请求封装
 */
public class NetCtrl {
    private static final String TAG = "TAG_NetCtrl";

    private NetModel mNetModel = NetModel.getInstance();

    public NetCtrl() {
        NetModel.setRequestResponseLogEnable(true);
    }

    /**
     * 根据ISBN号查询书籍信息
     *
     * @param isbn          书籍ISBN号
     * @param onNetCallback 回调
     */
    public void queryBookByISBN(String isbn, OnNetCallback onNetCallback) {
        try {
            mNetModel.getAsynch("http://118.31.113.49/api/isbn/v1/index?key=d7ba9fa7634764f2fd5bb81e8183ce18&isbn=" + isbn, onNetCallback);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "onFailure : " + e.getMessage());
        }
    }

    /**
     * 根据ISBN号查询书籍信息
     *
     * @param isbn          书籍ISBN号
     * @param onNetCallback 回调
     */
    public void queryBill(String isbn, OnNetCallback onNetCallback) {
        try {
            mNetModel.getAsynch("http://118.31.113.49/api/isbn/v1/index?key=d7ba9fa7634764f2fd5bb81e8183ce18&isbn=" + isbn, onNetCallback);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "onFailure : " + e.getMessage());
        }
    }

    public void addBook(OnNetCallback onNetCallback) {
        Map<String, String> headers = new HashMap<>();
        headers.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NjI5MTYzODQsImtleV91c2VyX2lkIjoxOTc2NTQ1NDE4NjUyMjQxOTIxfQ.q4HEvXz2aG8HdF4_b3s08GRi10P9XoTlanr8Tgz3-xY");

        mNetModel.postRequestJsonAsynch("http://192.168.110.82:8080/book/", headers, "{\n" +
                "  \"bookName\": \"22222\",\n" +
                "  \"bookColor\": 123\n" +
                "}", onNetCallback);
    }

    public void deleteBook(OnNetCallback onNetCallback) {
        Map<String, String> headers = new HashMap<>();
        headers.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NjI5MTYzODQsImtleV91c2VyX2lkIjoxOTc2NTQ1NDE4NjUyMjQxOTIxfQ.q4HEvXz2aG8HdF4_b3s08GRi10P9XoTlanr8Tgz3-xY");

        List<String> pathSegments = new ArrayList<>();
        pathSegments.add(String.valueOf(1980236678969757698L));

        mNetModel.deleteAsynch("http://192.168.110.82:8080/book/", pathSegments, null, headers, onNetCallback);
    }

    public void updateBook(OnNetCallback onNetCallback) {
        Map<String, String> headers = new HashMap<>();
        headers.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NjI5MTYzODQsImtleV91c2VyX2lkIjoxOTc2NTQ1NDE4NjUyMjQxOTIxfQ.q4HEvXz2aG8HdF4_b3s08GRi10P9XoTlanr8Tgz3-xY");

        List<String> pathSegments = new ArrayList<>();
        pathSegments.add(String.valueOf(1980233553378611201L));

        mNetModel.putRequestJsonAsynch("http://192.168.110.82:8080/book/", pathSegments, null, headers, "{\n" +
                "  \"bookName\": \"22222\",\n" +
                "  \"bookColor\": 6666\n" +
                "}", onNetCallback);
    }

    public void syncBook(long time, OnNetCallback onNetCallback) {
        Map<String, String> headers = new HashMap<>();
        headers.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NjI5MTYzODQsImtleV91c2VyX2lkIjoxOTc2NTQ1NDE4NjUyMjQxOTIxfQ.q4HEvXz2aG8HdF4_b3s08GRi10P9XoTlanr8Tgz3-xY");

        Map<String, String> queryParam = new HashMap<>();
        queryParam.put("startModifyTime", String.valueOf(time));
        mNetModel.getAsynch("http://192.168.110.82:8080/book/sync", null, queryParam, headers, onNetCallback);
    }
}
